package com.icebergteam.warfacewidget.rank.ui;

import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.icebergteam.warfacewidget.R;
import com.icebergteam.warfacewidget.rank.RankHelper;
import com.icebergteam.warfacewidget.rank.data.Top100ClansItem;

public class Top100ClansModelAdapter extends ArrayAdapter<Top100ClansItem> {
	public final String className = Top100ClansModelAdapter.class.getCanonicalName();
	
	private ArrayList<Top100ClansItem> listTop100ClansItems;
	
	public Top100ClansModelAdapter(Context context, int textViewResourceId, ArrayList<Top100ClansItem> top100Clans) {
		super(context, textViewResourceId, top100Clans);
		
		this.listTop100ClansItems = top100Clans;
	}
	
	public static View getViewWithParam(Context context, View convertView, Top100ClansItem top100ClansItem) {
		View v = convertView;
		if(v == null) {
			LayoutInflater vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = vi.inflate(R.layout.top100clans_list_item_widget_layout, null);
		}
		
		if(top100ClansItem != null) {
			
			ImageView rankImageView = (ImageView) v.findViewById(R.id.widget_rank_top100clans_list_item_rank_image);
			TextView clanNameTextView = (TextView) v.findViewById(R.id.widget_rank_top100clans_list_item_clanname);
			TextView ownerNameTextView = (TextView) v.findViewById(R.id.widget_rank_top100clans_list_item_ownernickname);
			TextView posTextView = (TextView) v.findViewById(R.id.widget_rank_top100clans_list_item_pos);
			TextView pos2TextView = (TextView) v.findViewById(R.id.widget_rank_top100clans_list_item_pos2);
			TextView pos3TextView = (TextView) v.findViewById(R.id.widget_rank_top100clans_list_item_pos3);
			
			if(top100ClansItem.getPos() > 3) {
				posTextView.setText(String.format("%s",top100ClansItem.getPos()));
				posTextView.setVisibility(View.VISIBLE);
				rankImageView.setVisibility(View.GONE);
			} else {
				rankImageView.setImageResource(RankHelper.getImageIdForRank(top100ClansItem.getPos()));
				posTextView.setVisibility(View.GONE);
				rankImageView.setVisibility(View.VISIBLE);
			}
				
			clanNameTextView.setText(top100ClansItem.getClanName());
			ownerNameTextView.setText(top100ClansItem.getOwnerNickname());
			
			pos2TextView.setText(String.format("%s",top100ClansItem.getPos2()));
			pos3TextView.setText(String.format("%s",top100ClansItem.getPos3()));
				
					
		}
		return v;
	}	

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		return getViewWithParam(getContext(), convertView, listTop100ClansItems.get(position));
	}

	
}